package com.service.yzj.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.service.yzj.dao.COrderMapper;
import com.service.yzj.dao.CProductMapper;
import com.service.yzj.dao.CustomMapper;
import com.service.yzj.model.basic.ImageItem;
import com.service.yzj.model.custom.CustomAddressInfo;
import com.service.yzj.model.custom.CustomUserInfo;
import com.service.yzj.model.order.OrderAppointment;
import com.service.yzj.model.order.OrderAppointmentDetail;
import com.service.yzj.model.order.OrderDetail;
import com.service.yzj.model.order.OrderInfo;
import com.service.yzj.model.order.OrderProduct;
import com.service.yzj.model.pages.wx.CustomPage;
import com.service.yzj.model.pages.wx.HomeModule;
import com.service.yzj.model.pages.wx.HomeModuleType;
import com.service.yzj.model.pages.wx.HomePage;
import com.service.yzj.model.pages.wx.SearchPage;
import com.service.yzj.model.product.ProductBasicInfo;
import com.service.yzj.model.product.ProductDetail;
import com.service.yzj.model.product.ProductInfo;
import com.service.yzj.model.product.ProductPkg;
import com.service.yzj.model.product.ProductTag;
import com.service.yzj.model.user.Employee;


@Service
public class WxServiceImpl implements WxService {
  private final CProductMapper cProductMapper;
  private final COrderMapper cOrderMapper;
  private final CustomMapper customMapper;
  private Logger logger;

  public WxServiceImpl(@Autowired CProductMapper cProductMapper, @Autowired COrderMapper cOrderMapper, @Autowired CustomMapper customMapper) {
    this.cProductMapper = cProductMapper;
    this.cOrderMapper = cOrderMapper;
    this.customMapper = customMapper;
    this.logger = LoggerFactory.getLogger(WxServiceImpl.class);
  }

 
  /**
   * 获取首页数据，/home
   */
  @Override
  public HomePage getHomePage() {
    HomePage homePage = new HomePage();    homePage.setPageTitle("首页");

    homePage.setCityCode("110000");

    // List<ImageItem> bannerList = cProductMapper.getBannerList();
    // HomeModule<ImageItem> bannerModule = new HomeModule<>(HomeModuleType.BANNER, bannerList);

    // List<ImageItem> iconList = cProductMapper.getIconList();
    // HomeModule<ImageItem> iconModule = new HomeModule<>(HomeModuleType.ICON, iconList);

    List<ProductBasicInfo> baseInfos = cProductMapper.getProductBaseInfo();
    List<ProductBasicInfo> productList = reformProductBasicInfos(baseInfos);
    HomeModule<ProductBasicInfo> productModule = new HomeModule<>(HomeModuleType.LIST, productList);

    homePage.setProductModule(productModule);

    // List<HomeTab> tabs = new HomeTab();
    // homePage.setTabs();
    return homePage;
  }

  /**
   * 搜索产品列表，/search
   */
  @Override
  public SearchPage getProductsWithKeyword(int pageNumber, int pageSize, String cityCode, String keyword) {

    int offset = (pageNumber - 1) * pageSize;

    List<ProductBasicInfo> baseInfos = cProductMapper.getProductsWithKeyword(offset, pageSize, cityCode, keyword);
    List<ProductBasicInfo> productList = reformProductBasicInfos(baseInfos);

    int pageTotal = cProductMapper.getProductsWithKeywordTotalCount(cityCode, keyword);

    SearchPage searchPage = new SearchPage();
    searchPage.setPageList(productList);
    searchPage.setPageNumber(pageNumber);
    searchPage.setPageSize(pageSize);
    searchPage.setPageTotal(pageTotal);

    return searchPage;
  }

  /**
   * 获取产品详情，/product
   */
  @Override
  public ProductInfo getProductWithId(long productId) {
    List<ProductBasicInfo> baseInfos = cProductMapper.getProductBaseInfoWithId(productId);
    if(!baseInfos.isEmpty()) {
      ProductBasicInfo baseInfo = baseInfos.get(0);
      baseInfo = reformProductBasicInfo(baseInfo);
      List<ImageItem> bannerList = cProductMapper.getProductImageInfo(productId, 2);
      List<ImageItem> imageList = cProductMapper.getProductImageInfo(productId, 3);

      ProductDetail productDetail = new ProductDetail();
      productDetail.setBasicInfo(baseInfo);
      productDetail.setBanners(bannerList);
      productDetail.setImages(imageList);

      List<ProductPkg> pkgs = cProductMapper.getProductPackageInfo(productId);

      ProductInfo productInfo = new ProductInfo();
      productInfo.setDetail(productDetail);
      productInfo.setPkgs(pkgs);
      return productInfo;
    }
    
    return null;
  }

  /**
   * 组装产品基本信息数据 单个
   */
  public ProductBasicInfo reformProductBasicInfo(ProductBasicInfo basicInfo) {
    long productId = basicInfo.getId();
    ImageItem imageInfo = getImageFromProductId(productId, 1);
    basicInfo.setImage(imageInfo);

    List<ProductTag> tags = cProductMapper.getProductTagInfo(productId);
    basicInfo.setTags(tags.toArray(new ProductTag[0]));
    return basicInfo;
  }

  /**
   * 组装产品基本信息数据 列表
   */
  public List<ProductBasicInfo> reformProductBasicInfos(List<ProductBasicInfo> baseInfos) {
    for (ProductBasicInfo info : baseInfos) {
      info = reformProductBasicInfo(info);
    }
    return baseInfos;
  }


  /**
   * 获取产品预览图片
   * @param productId
   * @param type
   * @return
   */
  public ImageItem getImageFromProductId(long productId, int type) {
    List<ImageItem> imageList = cProductMapper.getProductImageInfo(productId, type);
    if (!imageList.isEmpty()) {
        return imageList.get(0);
    }
    // 处理列表为空的情况
    return null;
  }

  /**
   * 获取我的订单，/my-order
   */
  @Override
  public OrderInfo getOrderWithUserId(long userId) {
    // 查询订单详情
    OrderDetail orderDetail = cOrderMapper.getOrderWithUser(userId);
    long orderId = orderDetail.getId();
    OrderAppointmentDetail appointmentDetail = cOrderMapper.getAppointmentDetailWithOrderId(orderId);
    // 查询地址信息
    long addressId = appointmentDetail.getAddressId();
    CustomAddressInfo address = cOrderMapper.getAddressById(addressId);
    // 查询员工信息
    long employeeId = appointmentDetail.getEmployeeId();
    Employee worker = cOrderMapper.getWorkerWithId(employeeId);
    // 组装预约数据
    OrderAppointment appointment = new OrderAppointment(appointmentDetail, address, worker);

    // 查询订单对应的产品信息
    List<ProductBasicInfo> baseInfos = cProductMapper.getProductListByOrderId(orderId);
    List<OrderProduct> productList = new ArrayList<>();
    for (ProductBasicInfo info : baseInfos) {
      info = reformProductBasicInfo(info);
      long productId = info.getId();
      ProductPkg packageSelected = cProductMapper.getSelectedPkgByOrderId(orderId, productId);
      OrderProduct orderProduct = new OrderProduct(info, packageSelected);
      productList.add(orderProduct);
    }

    // 组装订单数据
    OrderInfo orderInfo = new OrderInfo();
    orderInfo.setDetail(orderDetail);
    orderInfo.setAppointment(appointment);
    orderInfo.setProductList(productList);

    return orderInfo;
  }


  /**
   * 获取所有地址信息
   */
  @Override
  public List<CustomAddressInfo> getAllAddresses() {
    return cOrderMapper.getAllAddresses();
  }

  /**
   * 根据地址id获取地址信息
   */
  @Override
  public Optional<CustomAddressInfo> getAddressById(Integer id) {
    return Optional.ofNullable(cOrderMapper.getAddressById(id));
  }



  @Override
  public CustomPage getCustomerById(long userId) {
    CustomUserInfo user = customMapper.getCustomerById(userId);
    long addressId = user.getAddressId();
    CustomAddressInfo address = cOrderMapper.getAddressById(addressId);

    CustomPage customPage = new CustomPage(user, address, null, null, null);

    return customPage;
  }
}

